package com.machine.filling.function.init.InputOilStationNum;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.machine.filling.R;
import com.machine.filling.base.baseMvp.BaseFragment;
import com.machine.filling.db.dao.ClientInfoDao;
import com.machine.filling.function.init.OilGunList.OilGunListFragment;
import com.machine.filling.function.init.INextFragment;
import com.machine.filling.function.init.login.LoginFragment;
import com.machine.filling.net.http.response.OilMachineListResBean;
import com.machine.filling.net.http.response.OilStationInfoResBean;
import com.machine.filling.ui.dialog.ListDialog;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2018/1/11 0011.
 * 查询油站信息界面
 */
public class InputOilStationNumFragment extends BaseFragment<InputOilStationNumContract.Presenter> implements InputOilStationNumContract.View, View.OnClickListener {

    private EditText et_station_num;
    private TextView tv_station_info;
    private Button bt_ok;
    private Button bt_cancle;
    private Button bt_next;
    private TextView tv_machine;
    private INextFragment INextFragment;
    private List<OilMachineListResBean.ResultBean.MachinesBean> machinesList;//油机列表
    private OilMachineAdapter oilMachineAdapter;

    private String machineNum;//油机编号
    private String machineId;//油机id
    private OilStationInfoResBean.ResultBean.SellerBean stationLocalBean;
    private ListDialog listDialog;

    /**
     * @param mIntent
     */
    public InputOilStationNumFragment(Intent mIntent) {
        super(mIntent);
    }

    @Override
    public View initViews() {
        View view = LayoutInflater.from(mActivity).inflate(R.layout.fg_input_oil_station_num, null);
        et_station_num = (EditText) view.findViewById(R.id.et_station_num);
        tv_station_info = (TextView) view.findViewById(R.id.tv_station_info);
        tv_machine = (TextView) view.findViewById(R.id.tv_machine);
        bt_cancle = (Button) view.findViewById(R.id.bt_cancle);
        bt_ok = (Button) view.findViewById(R.id.bt_ok);
        bt_next = (Button) view.findViewById(R.id.bt_next);
        bt_cancle.setOnClickListener(this);
        bt_ok.setOnClickListener(this);
        tv_machine.setOnClickListener(this);
        bt_next.setOnClickListener(this);

        return view;
    }

    @Override
    public void initDatas() {
        mPresenter = new InputOilStationNumPresenter(mContext, this);
        machinesList = new ArrayList();
        oilMachineAdapter = new OilMachineAdapter(mContext, machinesList);
        oilMachineAdapter.setOnItemClickListener(new OilMachineAdapter.OnItemClickListener() {
            @Override
            public void onClickItem(int position, OilMachineListResBean.ResultBean.MachinesBean itemBean) {
                machineNum = itemBean.getNum();
                tv_machine.setText(machineNum);
                machineId = itemBean.getId();
                listDialog.dismiss();
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_cancle:
                INextFragment.replaceNext(new Intent(), LoginFragment.class.getName());
                break;
            case R.id.bt_ok:
                tv_machine.setText("");
                machineId = null;
                mPresenter.searchStationInfoByNum();
                break;
            case R.id.bt_next:
                mPresenter.bindOilMachineAndClient();
                break;
            case R.id.tv_machine:
                mPresenter.getOilMachineList();
                break;

        }
    }

    @Override
    public void showStationInfo(OilStationInfoResBean.ResultBean.SellerBean sellerBean) {
        stationLocalBean = sellerBean;
        tv_station_info.setText("商家名称：" + sellerBean.getName() + "\n商家编号：" + getStationNum() + "\n商家地址：" + sellerBean.getAddress());
    }

    @Override
    public String getStationNum() {
        return et_station_num.getText().toString().trim();
    }

    @Override
    public String getMachineId() {
        return machineId;
    }

    @Override
    public void showOilMachineList(List<OilMachineListResBean.ResultBean.MachinesBean> machinesList) {
        this.machinesList.clear();
        this.machinesList.addAll(machinesList);
        if (listDialog == null) {
            listDialog = new ListDialog(mContext);
            listDialog.setListTitle("选择油机");
            listDialog.setAdapter(oilMachineAdapter);
        }
        listDialog.show();
        oilMachineAdapter.notifyDataSetChanged();
    }

    @Override
    public void showBindSuccess() {
        saveStationAndMachineInfo();
        INextFragment.replaceNext(new Intent(), OilGunListFragment.class.getName());
    }

    /**
     * 保存油站和油机的信息
     */
    private void saveStationAndMachineInfo() {
        ClientInfoDao.getInstance().updataStationId(stationLocalBean.getId());
        ClientInfoDao.getInstance().updataStationName(stationLocalBean.getName());
        ClientInfoDao.getInstance().updataStationAddr(stationLocalBean.getAddress());
        ClientInfoDao.getInstance().updataStationNum(getStationNum());
        ClientInfoDao.getInstance().updataMachineId(machineId);
        ClientInfoDao.getInstance().updataMachineNum(machineNum);
    }

    public void setINextFragment(INextFragment INextFragment) {
        this.INextFragment = INextFragment;
    }
}
